/* -+- file  ./kf2/Events/ChatEvent.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using System.Collections.Generic;
using Habazie.kf2.Core;
using Habazie.kf2.Net;
using Habazie.kf2.GUI;

namespace Habazie.kf2.Events
{
	
	public class ChatEvent : Event
	{
		private LinkedList<jabber.protocol.client.Message> m_Messages;
		private jabber.JID m_JID;
		
		public ChatEvent(Account source, jabber.protocol.client.Message msg)
			: base (source, msg)
		{
			m_Messages = new LinkedList<jabber.protocol.client.Message> ();
			m_Messages.AddLast (msg);
			m_JID = msg.From;
			m_EventName = "chat";

			if (msg.Body != null)	// TODO
				Util.PlaySound ("chat-in");
		}
		

		
		public jabber.JID GetJID ()
		{
			return m_JID;
		}
		
		
		///<summary>Display this event</summary>
		public override void Display ()
		{
		//	MessageWindow win = new MessageWindow ();
		//	win.Msg = m_Message;
		//	win.Show ();
			Chat c = Chat.Get (Source, m_JID);
//			if (c == null)
	//			c = new Chat (Source, m_JID);
			c.Show ();
			foreach (jabber.protocol.client.Message msg in m_Messages) {
				c.DisplayMsg (msg as Message);
			}
			
		}
		
		
		///<summary>Notify user of event</summary>
		public override void Notify (INotifyService notify)
		{
			string header;
			if (this.SourceItem != null)
				header = this.SourceItem.Name;
			else
				header = m_JID;
				
			// TODO: crop message if necessary
			if (m_Messages.Last.Value.Body != null)
				notify.Notify (NotifyClass.Chat, header, Msg.Body);
		}
		
		public void Push (jabber.protocol.client.Message msg)
		{
		//	if (msg.Body != null)
		//		Util.PlaySound ("chat-in");
			m_Messages.AddLast (msg);
		}
		
		
		public jabber.protocol.client.Message Msg {
			get { return m_Messages.First.Value; }
		}
		
		///<summary>Get string representation of this event</summary>
		public override string ToString ()
		{
			return m_Messages.First.Value.From.ToString ();
		}
	}
}
